package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.promote.Topic;
import com.cskaoyan.bean.promote.base.PromotePageData;
import com.cskaoyan.bean.promote.base.PromoteParam;
import com.cskaoyan.bean.promote.bo.BatchDeleteTopicBO;
import com.cskaoyan.bean.promote.bo.TopicListSearchBO;
import com.cskaoyan.bean.promote.exception.*;
import com.cskaoyan.bean.promote.utils.PromoteValidationUtil;
import com.cskaoyan.bean.promote.vo.EchoUpdateTopicVO;
import com.cskaoyan.service.promote.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * @author liXiaoFei
 * @date 2021/10/18 17:06
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;
    @RequiresPermissions("admin:ad:list")
    @GetMapping("list")
    public BaseRespVo list(PromoteParam promoteParam, TopicListSearchBO topicListSearchBO){
        PromotePageData data = topicService.queryTopicList(promoteParam,topicListSearchBO);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:ad:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated Topic topic, BindingResult bindingResult) throws PromoteParamException, AddTopicException {
        PromoteValidationUtil.valid(bindingResult);
        Topic topicVO = topicService.addTopic(topic);
        return BaseRespVo.ok(topicVO);
    }
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Topic topic) throws DeleteTopicException {
        topicService.deleteTopic(topic);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:ad:batch-delete")
    @PostMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody BatchDeleteTopicBO ids) throws DeleteTopicException {
        topicService.batchDeleteTopic(ids);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:ad:read")
    @GetMapping("read")
    public BaseRespVo read(Integer id) throws EchoUpdateTopicException {
        EchoUpdateTopicVO echoUpdateTopicVO = topicService.echoUpdateTopic(id);
        return BaseRespVo.ok(echoUpdateTopicVO);
    }
    @RequiresPermissions("admin:ad:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Topic topic) throws UpdateTopicException {
        Topic topicVO = topicService.updateTopic(topic);
        return BaseRespVo.ok(topicVO);
    }

}
